import { contextBridge, ipcRenderer } from 'electron';

// 安全地暴露IPC功能到渲染进程
contextBridge.exposeInMainWorld('electronAPI', {
  // AI聊天相关
  aiChat: (message: string) => ipcRenderer.invoke('ai-chat', message),
  getChatHistory: () => ipcRenderer.invoke('get-chat-history'),
  getHistory: () => ipcRenderer.invoke('get-chat-history'),
  clearChatHistory: () => ipcRenderer.invoke('clear-chat-history'),
  getHistoryList: () => ipcRenderer.invoke('get-history-list'),
  loadSessionToChat: (sessionId: string) => ipcRenderer.invoke('load-session-to-chat', sessionId),
  getPrompts: () => ipcRenderer.invoke('get-prompts'),
  switchPrompt: (index: number) => ipcRenderer.invoke('switch-prompt', index),
  abortChat: () => ipcRenderer.invoke('abort-chat'),
  
  // 系统功能
  helloWorld: (name?: string) => ipcRenderer.invoke('hello-world', name),
  getSystemInfo: () => ipcRenderer.invoke('get-system-info'),
  getCurrentTime: () => ipcRenderer.invoke('get-current-time'),
  systemCall: (command: string) => ipcRenderer.invoke('system-call', command),

  // 流式响应监听器
  onAiStreamStart: (callback: (data: any) => void) => {
    ipcRenderer.on('ai-stream-start', (_, data) => callback(data));
  },
  onAiStreamContent: (callback: (data: any) => void) => {
    ipcRenderer.on('ai-stream-content', (_, data) => callback(data));
  },
  onAiStreamComplete: (callback: (data: any) => void) => {
    ipcRenderer.on('ai-stream-complete', (_, data) => callback(data));
  },
  onAiReasoning: (callback: (data: any) => void) => {
    ipcRenderer.on('ai-reasoning', (_, data) => callback(data));
  },
  onAiReasoningEnd: (callback: (data: any) => void) => {
    ipcRenderer.on('ai-reasoning-end', (_, data) => callback(data));
  },
  onAiToolExecutionStart: (callback: (data: any) => void) => {
    ipcRenderer.on('ai-tool-execution-start', (_, data) => callback(data));
  },
  onAiToolExecutionComplete: (callback: (data: any) => void) => {
    ipcRenderer.on('ai-tool-execution-complete', (_, data) => callback(data));
  },
  onAiError: (callback: (data: any) => void) => {
    ipcRenderer.on('ai-error', (_, data) => callback(data));
  },

  // 清理监听器
  removeAllListeners: () => {
    ipcRenderer.removeAllListeners('ai-stream-start');
    ipcRenderer.removeAllListeners('ai-stream-content');
    ipcRenderer.removeAllListeners('ai-stream-complete');
    ipcRenderer.removeAllListeners('ai-reasoning');
    ipcRenderer.removeAllListeners('ai-reasoning-end');
    ipcRenderer.removeAllListeners('ai-tool-execution-start');
    ipcRenderer.removeAllListeners('ai-tool-execution-complete');
    ipcRenderer.removeAllListeners('ai-error');
  }
});

// 类型声明
declare global {
  interface Window {
    electronAPI: {
      // AI聊天相关
      aiChat: (message: string) => Promise<any>;
      getChatHistory: () => Promise<any>;
      clearChatHistory: () => Promise<any>;
      getPrompts: () => Promise<any>;
      switchPrompt: (index: number) => Promise<any>;
      abortChat: () => Promise<any>;

      // 系统功能
      helloWorld: (name?: string) => Promise<any>;
      getSystemInfo: () => Promise<any>;
      getCurrentTime: () => Promise<any>;
      systemCall: (command: string) => Promise<any>;

      // 流式响应监听器
      onAiStreamStart: (callback: (data: any) => void) => void;
      onAiStreamContent: (callback: (data: any) => void) => void;
      onAiStreamComplete: (callback: (data: any) => void) => void;
      onAiReasoning: (callback: (data: any) => void) => void;
      onAiReasoningEnd: (callback: (data: any) => void) => void;
      onAiToolExecutionStart: (callback: (data: any) => void) => void;
      onAiToolExecutionComplete: (callback: (data: any) => void) => void;
      onAiError: (callback: (data: any) => void) => void;

      // 清理监听器
      removeAllListeners: () => void;
    };
  }
}